﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Resources;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Converter;
using csla4Skeleton.UI.SL.Resources.Utility;
using csla4Skeleton.UI.SL.DanhMuc.DM_HangHoa;
using csla4Skeleton.UI.SL.DanhMuc.DM_Kho;
using csla4Skeleton.UI.SL.NghiepVu.Kho.K_ListNhapKho;
using csla4Skeleton.UI.SL.NghiepVu.Kho.K_ListXuatKho;
using csla4Skeleton.UI.SL.HeThong.HT_ThongTinCongTy;
using csla4Skeleton.UI.SL.NghiepVu.BanSanPham.BSP_BanHangHoa;
using Accounting.Library;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using System.ComponentModel;
using Utility;
using Accounting.Library.Utility;

namespace csla4Skeleton.UI.SL.NghiepVu.Kho.K_TonKho
{
    public partial class K_TonKho_MainWindow : UserControl, Csla.Xaml.ISupportNavigation
    {

        #region ISupportNavigation Members
        List<Control> controls = new List<Control>();
        public bool CreateBookmarkAfterLoadCompleted
        {
            get { return true; }
        }

        public event EventHandler LoadCompleted;

        protected virtual void OnLoadCompleted()
        {
            if (LoadCompleted != null)
                LoadCompleted(this, EventArgs.Empty);
        }

        public void SetParameters(string parameters)
        {
            try
            {
                if (parameters == "")
                    Khoa = null;
                else
                    Khoa = Int32.Parse(parameters);
            }
            catch
            {
                Khoa = null;
            }
        }

        public string Title
        {
            get { return "TonKho"; }
        }


        #endregion

        #region Load Methods
        private System.Nullable<int> Khoa = null;
        List<K_KhoInfo> NewListTonKho = new List<K_KhoInfo>();
        public K_TonKho_MainWindow(int Khoa)
        {
            this.Khoa = Khoa;
        }

        public K_TonKho_MainWindow()
        {
            LocalizationConverter converter = new LocalizationConverter();
            converter.LocalResourceManager = new ResourceManager("csla4Skeleton.UI.SL.NghiepVu.Kho.K_TonKho.Resources", Assembly.GetExecutingAssembly());
            this.Resources.Add("Localization", converter);

            InitializeComponent();
            ControlBehavior.ControlBehaviors.Navigation(this.K_TonKho_GeneralInformation_GroupBox, controls);

        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {

                System.Windows.Data.CollectionViewSource K_TonKho_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["K_TonKho_ViewModelSource"];
                if (Khoa == null)
                {
                    K_TonKho_ViewModel K_TonKho_ViewModel = new K_TonKho_ViewModel();
                    K_TonKho_ViewModel.ChildList_AddedNew += new K_TonKho_ViewModel.ChildList_AddedNew_EventHandler(K_TonKho_ViewModel_ChildList_AddedNew);
                    K_TonKho_ViewModel.TimeChanged += new EventHandler(OnTimeChanged);
                    K_TonKho_ViewModel.CountChanged += new EventHandler(OnCountChanged);
                    K_TonKho_CollectionViewSource.Source = new List<object> { K_TonKho_ViewModel };


                }
                else
                {
                    K_TonKho_ViewModel K_TonKho_ViewModel = new K_TonKho_ViewModel((int)Khoa);
                    K_TonKho_ViewModel.ChildList_AddedNew += new K_TonKho_ViewModel.ChildList_AddedNew_EventHandler(K_TonKho_ViewModel_ChildList_AddedNew);
                    K_TonKho_ViewModel.TimeChanged += new EventHandler(OnTimeChanged);
                    K_TonKho_ViewModel.CountChanged += new EventHandler(OnCountChanged);
                    K_TonKho_CollectionViewSource.Source = new List<object> { K_TonKho_ViewModel };
                }

                System.Windows.Data.CollectionViewSource dm_HangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_HangHoa_ListViewModelSource"];
                DM_HangHoa_ListViewModel dm_HangHoa_ListViewModel = new DM_HangHoa_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_HangHoa_CollectionViewSource.Source = new List<object> { dm_HangHoa_ListViewModel };

                System.Windows.Data.CollectionViewSource ht_ThongTinCongTy_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["HT_ThongTinCongTy_ListViewModelSource"];
                HT_ThongTinCongTy_ListViewModel ht_ThongTinCongTy_ListViewModel = new HT_ThongTinCongTy_ListViewModel(BusinessConfiguration.Configuration.GridViewFetchMode);
                ht_ThongTinCongTy_CollectionViewSource.Source = new List<object> { ht_ThongTinCongTy_ListViewModel };

                System.Windows.Data.CollectionViewSource k_ListNhapKho_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["K_ListNhapKho_ListViewModelSource"];
                K_ListNhapKho_ListViewModel k_ListNhapKho_ListViewModel = new K_ListNhapKho_ListViewModel(DateTime.Now);
                k_ListNhapKho_CollectionViewSource.Source = new List<object> { k_ListNhapKho_ListViewModel };

                System.Windows.Data.CollectionViewSource k_ListXuatKho_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["K_ListXuatKho_ListViewModelSource"];
                K_ListXuatKho_ListViewModel k_ListXuatKho_ListViewModel = new K_ListXuatKho_ListViewModel(DateTime.Now);
                k_ListXuatKho_CollectionViewSource.Source = new List<object> { k_ListXuatKho_ListViewModel };

                System.Windows.Data.CollectionViewSource K_TonKho_ThangTruoc_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["K_TonKho_ThangTruoc_ViewModelSource"];
                K_TonKho_ViewModel K_TonKho_ThangTruoc_ViewModel = new K_TonKho_ViewModel(DateTime.Now.AddMonths(-1));
                K_TonKho_ThangTruoc_CollectionViewSource.Source = new List<object> { K_TonKho_ThangTruoc_ViewModel };


                System.Windows.Data.CollectionViewSource dm_Kho_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_Kho_ListViewModelSource"];
                DM_Kho_ListViewModel dm_Kho_ListViewModel = new DM_Kho_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_Kho_CollectionViewSource.Source = new List<object> { dm_Kho_ListViewModel };

                this.K_TonKho_TonKhoChiTietGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.K_TonKho_TonKhoChiTietGridView);

                ((ControlBehavior.GridViewKeyboardCommandProvider)this.K_TonKho_TonKhoChiTietGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);

            }
        }

        #endregion


        #region Event Handle
        private void LayoutRoot_KeyUp(object sender, KeyEventArgs e)
        {
            if (Keyboard.Modifiers == ModifierKeys.Control)
            {
                K_TonKho_ViewModel vm = GetViewModel();
                if (e.Key == Key.S)
                {
                    if (vm.CanSave)
                    {
                        vm.Save(sender, new Csla.Xaml.ExecuteEventArgs());
                    }
                }

                else if (e.Key == Key.D1)
                {
                    this.K_TonKho_TonKhoChiTietTab.IsSelected = true;
                }



            }
            e.Handled = true;
        }



        private RadGridView GetActiveGridView()
        {
            if (this.K_TonKho_TonKhoChiTietTab.IsSelected)
            {
                return K_TonKho_TonKhoChiTietGridView;
            }


            return null;
        }




        void GridView_AddingNewDataItem(object sender, GridViewAddingNewEventArgs e)
        {
            var grid = e.OwnerGridViewItemsControl;
            if (grid.Items.Count > 0)
            {
                GridViewUtil.FocusRow((RadGridView)grid, grid.Items.Count - 1);
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    grid.CurrentCellInfo = new GridViewCellInfo(grid.Items[grid.Items.Count - 1], grid.Columns[0]);
                    grid.Focus();
                    grid.BeginEdit();
                }));
            }
        }

        private void SaveAndNewButton_Click(object sender, RoutedEventArgs e)
        {
            K_TonKho_ViewModel vm = GetViewModel();
            vm.SaveAndNew();
        }

        private void DatePicker_KeyUp(object sender, KeyEventArgs e)
        {
            List<Control> availableControl = new List<Control>();
            foreach (Control item in controls)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    availableControl.Add(item);
                }
            }
            Control[] list = availableControl.ToArray();
            if (sender.GetHashCode() == list[availableControl.Count - 1].GetHashCode())
            {
                if (e.Key == Key.Enter)
                {
                    this.AddTonKhoDetailsItem();
                }
            }
            else
            {
                ControlBehavior.DatePickerBehaviors.DatePicker_KeyUp(sender, e, controls);
            }
        }

        private void ComboBox_KeyUp(object sender, KeyEventArgs e)
        {
            List<Control> availableControl = new List<Control>();
            foreach (Control item in controls)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    availableControl.Add(item);
                }
            }
            Control[] list = availableControl.ToArray();
            if (sender.GetHashCode() == list[availableControl.Count - 1].GetHashCode())
            {
                if (e.Key == Key.Enter)
                {
                    this.AddTonKhoDetailsItem();
                }
            }
            else
            {
                ControlBehavior.ComboBoxBehaviors.ComboBox_KeyUp(sender, e, controls);
            }
        }

        private void Control_KeyUp(object sender, KeyEventArgs e)
        {
            List<Control> availableControl = new List<Control>();
            foreach (Control item in controls)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    availableControl.Add(item);
                }
            }
            Control[] list = availableControl.ToArray();
            if (sender.GetHashCode() == list[availableControl.Count - 1].GetHashCode())
            {
                if (e.Key == Key.Enter)
                {
                    this.AddTonKhoDetailsItem();
                }
            }
            else
            {
                ControlBehavior.ControlBehaviors.Control_KeyUp(sender, e, controls);
            }
        }



        void OnReachLastColumn(object sender, ReachLastColumnArgs e)
        {

            this.tabControl.SelectedItem = tabControl.Items[0];
            FocusAfterChangeTab(this.K_TonKho_TonKhoChiTietGridView, e.Index);
        }

        private void FocusAfterChangeTab(GridViewDataControl grid, int index)
        {

            Dispatcher.BeginInvoke(new Action(() =>
            {
                grid.CurrentCellInfo = new GridViewCellInfo(grid.Items[index], grid.Columns[0]);
                grid.Focus();
                grid.BeginEdit();
            }));


        }


        #endregion

        #region View Model Related Methods
        private K_TonKho_ViewModel GetViewModel()
        {
            System.Windows.Data.CollectionViewSource K_TonKho_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["K_TonKho_ViewModelSource"];
            return ((List<object>)K_TonKho_CollectionViewSource.Source)[0] as K_TonKho_ViewModel;
        }

        public void OnTimeChanged(object sender, EventArgs e)
        {
            /*this.GetViewModel().Model.Thang = this.GetViewModel().Model.NgayTao.Month;
            this.GetViewModel().Model.Nam = this.GetViewModel().Model.NgayTao.Year;*/

            System.Windows.Data.CollectionViewSource k_ListXuatKho_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["K_ListXuatKho_ListViewModelSource"];
            K_ListXuatKho_ListViewModel k_ListXuatKho_ListViewModel = new K_ListXuatKho_ListViewModel(this.GetViewModel().Model.NgayTao);
            k_ListXuatKho_CollectionViewSource.Source = new List<object> { k_ListXuatKho_ListViewModel };

            System.Windows.Data.CollectionViewSource k_ListNhapKho_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["K_ListNhapKho_ListViewModelSource"];
            K_ListNhapKho_ListViewModel k_ListNhapKho_ListViewModel = new K_ListNhapKho_ListViewModel(this.GetViewModel().Model.NgayTao);
            k_ListNhapKho_CollectionViewSource.Source = new List<object> { k_ListNhapKho_ListViewModel };

            System.Windows.Data.CollectionViewSource K_TonKho_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["K_TonKho_ThangTruoc_ViewModelSource"];
            K_TonKho_ViewModel K_TonKho_ViewModel = new K_TonKho_ViewModel(this.GetViewModel().Model.NgayTao.AddMonths(-1));
            K_TonKho_CollectionViewSource.Source = new List<object> { K_TonKho_ViewModel };


        }



        private void AddTonKhoDetailsItem()
        {
            GetViewModel().AddK_TonKho_ChiTietItem();

        }

        private void DeleteMuaHangDetailsItem(int Index)
        {
            GetViewModel().DeleteK_TonKho_ChiTietItem(Index);
        }


        void K_TonKho_ViewModel_ChildList_AddedNew(object sender, ChildListAddedNewArgs e)
        {
            if (e.ChildListName == "K_TonKho_ChiTiets")
            {
                this.K_TonKho_TonKhoChiTietGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
        }
        #endregion

        private void TinhTonKho_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Data.CollectionViewSource ht_ThongTinCongTy_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["HT_ThongTinCongTy_ListViewModelSource"];
            HT_ThongTinCongTy_ListViewModel ht_ThongTinCongTy_ListViewModel = ((List<object>)ht_ThongTinCongTy_CollectionViewSource.Source)[0] as HT_ThongTinCongTy_ListViewModel;
            if (ht_ThongTinCongTy_ListViewModel.Model.getListOfInfo().Count == 0)
            {
                DoTinhTonKho("NTXT");
            }
            else
            {
                if (ht_ThongTinCongTy_ListViewModel.Model.getListOfInfo()[0].PhuongPhapTinhGiaVonId == 1)
                {
                    DoTinhTonKho("NTXT");
                }
                else if (ht_ThongTinCongTy_ListViewModel.Model.getListOfInfo()[0].PhuongPhapTinhGiaVonId == 2)
                {
                    DoTinhTonKho("NSXT");
                }
                else if (ht_ThongTinCongTy_ListViewModel.Model.getListOfInfo()[0].PhuongPhapTinhGiaVonId == 3)
                {
                    DoTinhTonKhoBinhQuanTucThi();
                }
                else if (ht_ThongTinCongTy_ListViewModel.Model.getListOfInfo()[0].PhuongPhapTinhGiaVonId == 4)
                {
                    DoTinhTonKhoBinhQuanGiaQuyen();
                }

            }
        }

        public void DoTinhTonKhoBinhQuanGiaQuyen()
        {
            //IList<K_ListNhapKhoInfo> listNhapKho = null;
            //IList<K_ListXuatKhoInfo> listXuatKho = null;

            //System.Windows.Data.CollectionViewSource k_ListNhapKho_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["K_ListNhapKho_ListViewModelSource"];
            //K_ListNhapKho_ListViewModel k_ListNhapKho_ListViewModel = ((List<object>)k_ListNhapKho_CollectionViewSource.Source)[0] as K_ListNhapKho_ListViewModel;

            //if (k_ListNhapKho_ListViewModel.Model != null)
            //{
            //    listNhapKho = k_ListNhapKho_ListViewModel.Model.getListOfInfo();
            //}

            //System.Windows.Data.CollectionViewSource k_ListXuatKho_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["K_ListXuatKho_ListViewModelSource"];
            //K_ListXuatKho_ListViewModel k_ListXuatKho_ListViewModel = ((List<object>)k_ListXuatKho_CollectionViewSource.Source)[0] as K_ListXuatKho_ListViewModel;

            //if (k_ListXuatKho_ListViewModel.Model != null)
            //{
            //    listXuatKho = k_ListXuatKho_ListViewModel.Model.getListOfInfo();
            //}


            //List<K_KhoInfo> ListXuatKhoRutGon = new List<K_KhoInfo>();

            //if (listXuatKho != null)
            //{
            //    foreach (K_ListXuatKhoInfo item in listXuatKho)
            //    {
            //        bool IsExist = false;
            //        foreach (K_KhoInfo KhoInfo in ListXuatKhoRutGon)
            //        {
            //            if (KhoInfo.HangHoaId == item.HangHoaId)
            //            {
            //                IsExist = true;
            //                if (item.SoLuong != null)
            //                {
            //                    KhoInfo.SoLuong += item.SoLuong.Value; 
            //                    break;
            //                }
            //            }
            //        }
            //        if (!IsExist)
            //        {
            //            K_KhoInfo khoInfo = new K_KhoInfo(item.HangHoaId, item.SoLuong);
            //            ListXuatKhoRutGon.Add(khoInfo);
            //        }
            //    }
            //}

            //List<K_KhoInfo> listHangTonVaNhap = new List<K_KhoInfo>();

            //if (listNhapKho != null)
            //{
            //    foreach (K_ListNhapKhoInfo item in listNhapKho)
            //    {
            //        bool IsExist = false;
            //        foreach (K_KhoInfo KhoInfo in listHangTonVaNhap)
            //        {
            //            if (KhoInfo.HangHoaId == item.HangHoaId)
            //            {
            //                IsExist = true;
            //                if (item.SoLuong != null)
            //                {
            //                    KhoInfo.DonGia = (KhoInfo.DonGia * KhoInfo.SoLuong + item.SoLuong * item.DonGiaQĐ) / (item.SoLuong + KhoInfo.SoLuong);
            //                    KhoInfo.SoLuong += item.SoLuong.Value;                    
            //                    break;
            //                }
            //            }
            //        }
            //        if (!IsExist)
            //        {
            //            K_KhoInfo khoInfo = new K_KhoInfo(item.HangHoaId, item.SoLuong,item.DonGiaQĐ);
            //            listHangTonVaNhap.Add(khoInfo);
            //        }
            //    }
            //}


            //System.Windows.Data.CollectionViewSource K_TonKho_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["K_TonKho_ThangTruoc_ViewModelSource"];
            //K_TonKho_ViewModel K_TonKho_ViewModel = ((List<object>)K_TonKho_CollectionViewSource.Source)[0] as K_TonKho_ViewModel;


            //int countListHangTonVaNhap = listHangTonVaNhap.Count;

            //if (K_TonKho_ViewModel.Model.Khoa != 0)
            //{
            //    foreach (K_TonKho_ChiTietChild item in K_TonKho_ViewModel.Model.K_TonKho_ChiTiets)
            //    {
            //        bool IsHangHoaExist = false;
            //        for (int i = 0; i < countListHangTonVaNhap; i++)
            //        {
            //            if (listHangTonVaNhap[i].HangHoaId == item.HangHoaId)
            //            {
            //                listHangTonVaNhap[i].DonGia = (listHangTonVaNhap[i].DonGia * listHangTonVaNhap[i].SoLuong + item.SoLuong * item.DonGia) / (item.SoLuong + listHangTonVaNhap[i].SoLuong);
            //                listHangTonVaNhap[i].SoLuong += item.SoLuong;
            //                IsHangHoaExist = true;
            //                break;
            //            }
            //        }
            //        if (!IsHangHoaExist)
            //        {
            //            K_KhoInfo khoInfo = new K_KhoInfo(item.HangHoaId, item.SoLuong);
            //            listHangTonVaNhap.Add(khoInfo);
            //        }
            //    }
            //}


            //foreach (K_KhoInfo TonVaNhapItem in listHangTonVaNhap)
            //{
            //    foreach (K_KhoInfo XuatKhoItem in ListXuatKhoRutGon)
            //    {
            //        if (XuatKhoItem.HangHoaId == TonVaNhapItem.HangHoaId && XuatKhoItem.SoLuong != null)
            //        {
            //            TonVaNhapItem.SoLuong -= XuatKhoItem.SoLuong;
            //            break;
            //        }
            //    }
            //    K_KhoInfo KhoInfo = new K_KhoInfo(TonVaNhapItem.HangHoaId, TonVaNhapItem.SoLuong, TonVaNhapItem.DonGia);
            //    NewListTonKho.Add(KhoInfo);
            //    this.AddTonKhoDetailsItem();
            //}

        }
        public void DoTinhTonKho(string mode)
        {
            //IList<K_ListNhapKhoInfo> listNhapKho = null;
            //IList<K_ListXuatKhoInfo> listXuatKho = null;

            //System.Windows.Data.CollectionViewSource k_ListNhapKho_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["K_ListNhapKho_ListViewModelSource"];
            //K_ListNhapKho_ListViewModel k_ListNhapKho_ListViewModel = ((List<object>)k_ListNhapKho_CollectionViewSource.Source)[0] as K_ListNhapKho_ListViewModel;

            //if (k_ListNhapKho_ListViewModel.Model != null)
            //{
            //    listNhapKho = k_ListNhapKho_ListViewModel.Model.getListOfInfo();
            //}

            //System.Windows.Data.CollectionViewSource k_ListXuatKho_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["K_ListXuatKho_ListViewModelSource"];
            //K_ListXuatKho_ListViewModel k_ListXuatKho_ListViewModel = ((List<object>)k_ListXuatKho_CollectionViewSource.Source)[0] as K_ListXuatKho_ListViewModel;

            //if (k_ListXuatKho_ListViewModel.Model != null)
            //{
            //    listXuatKho = k_ListXuatKho_ListViewModel.Model.getListOfInfo();
            //}


            //List<List<K_KhoInfo>> ListXuatKhoRutGon = new List<List<K_KhoInfo>>();

            //if (listXuatKho != null)
            //{
            //    foreach (K_ListXuatKhoInfo item in listXuatKho)
            //    {
            //        bool IsExist = false;
            //        foreach (List<K_KhoInfo> ListKhoInfo in ListXuatKhoRutGon)
            //        {
            //            if (ListKhoInfo[0].HangHoaId == item.HangHoaId)
            //            {
            //                IsExist = true;
            //                if (item.SoLuong != null)
            //                {
            //                    K_KhoInfo khoInfo = new K_KhoInfo(item.HangHoaId, item.SoLuong, item.DonGia, item.NgayChungTu);
            //                    ListKhoInfo.Add(khoInfo);
            //                    break;

            //                }

            //            }
            //        }
            //        if (!IsExist)
            //        {
            //            List<K_KhoInfo> NewListKhoInfo = new List<K_KhoInfo>();
            //            K_KhoInfo khoInfo = new K_KhoInfo(item.HangHoaId, item.SoLuong, item.DonGia, item.NgayChungTu);
            //            NewListKhoInfo.Add(khoInfo);
            //            ListXuatKhoRutGon.Add(NewListKhoInfo);
            //        }
            //    }
            //}
            //for (int i = 0; i < ListXuatKhoRutGon.Count; i++)
            //{
            //    ListXuatKhoRutGon[i] = K_KhoInfo.NhapTruocXuatTruocSort(ListXuatKhoRutGon[i]);
            //}

            //List<List<K_KhoInfo>> listHangTonVaNhap = new List<List<K_KhoInfo>>();

            //if (listNhapKho != null)
            //{
            //    foreach (K_ListNhapKhoInfo item in listNhapKho)
            //    {
            //        bool IsExist = false;
            //        foreach (List<K_KhoInfo> ListKhoInfo in listHangTonVaNhap)
            //        {
            //            if (ListKhoInfo[0].HangHoaId == item.HangHoaId)
            //            {
            //                IsExist = true;
            //                if (item.SoLuong != null)
            //                {
            //                    K_KhoInfo khoInfo = new K_KhoInfo(item.HangHoaId, item.SoLuong, item.DonGiaQĐ, item.NgayChungTu);
            //                    ListKhoInfo.Add(khoInfo);
            //                    break;

            //                }

            //            }
            //        }
            //        if (!IsExist)
            //        {
            //            List<K_KhoInfo> NewListKhoInfo = new List<K_KhoInfo>();
            //            K_KhoInfo khoInfo = new K_KhoInfo(item.HangHoaId, item.SoLuong, item.DonGiaQĐ, item.NgayChungTu);
            //            NewListKhoInfo.Add(khoInfo);
            //            listHangTonVaNhap.Add(NewListKhoInfo);
            //        }
            //    }
            //}


            //System.Windows.Data.CollectionViewSource K_TonKho_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["K_TonKho_ThangTruoc_ViewModelSource"];
            //K_TonKho_ViewModel K_TonKho_ViewModel = ((List<object>)K_TonKho_CollectionViewSource.Source)[0] as K_TonKho_ViewModel;


            //int countListHangTonVaNhap = listHangTonVaNhap.Count;

            //if (mode.Equals("NTXT"))
            //{

            //    if (K_TonKho_ViewModel.Model.Khoa != 0)
            //    {
            //        foreach (K_TonKho_ChiTietChild item in K_TonKho_ViewModel.Model.K_TonKho_ChiTiets)
            //        {
            //            bool IsHangHoaExist = false;
            //            for (int i = 0; i < countListHangTonVaNhap; i++)
            //            {
            //                if (listHangTonVaNhap[i][0].HangHoaId == item.HangHoaId)
            //                {
            //                    listHangTonVaNhap[i] = K_KhoInfo.NhapTruocXuatTruocSort(listHangTonVaNhap[i]);

            //                    int numberOfDays = DateTime.DaysInMonth(this.GetViewModel().Model.Nam, this.GetViewModel().Model.Thang);
            //                    DateTime lastDay = new DateTime(this.GetViewModel().Model.Nam, this.GetViewModel().Model.Thang, numberOfDays);

            //                    K_KhoInfo khoInfo = new K_KhoInfo(item.HangHoaId, item.SoLuong, item.DonGia, lastDay.AddMonths(-1));
            //                    listHangTonVaNhap[i].Insert(0, khoInfo);
            //                    IsHangHoaExist = true;
            //                }
            //            }
            //            if (!IsHangHoaExist)
            //            {

            //                int numberOfDays = DateTime.DaysInMonth(this.GetViewModel().Model.Nam, this.GetViewModel().Model.Thang);
            //                DateTime lastDay = new DateTime(this.GetViewModel().Model.Nam, this.GetViewModel().Model.Thang, numberOfDays);

            //                K_KhoInfo khoInfo = new K_KhoInfo(item.HangHoaId, item.SoLuong, item.DonGia, lastDay.AddMonths(-1));
            //                List<K_KhoInfo> listKhoInfo = new List<K_KhoInfo>();
            //                listKhoInfo.Add(khoInfo);
            //                listHangTonVaNhap.Add(listKhoInfo);
            //            }
            //        }
            //    }
            //    else
            //    {
            //        for (int i = 0; i < countListHangTonVaNhap; i++)
            //        {
            //            listHangTonVaNhap[i] = K_KhoInfo.NhapTruocXuatTruocSort(listHangTonVaNhap[i]);
            //        }
            //    }
            //}
            //else if (mode.Equals("NSXT"))
            //{
            //    if (K_TonKho_ViewModel.Model.Khoa != 0)
            //    {
            //        foreach (K_TonKho_ChiTietChild item in K_TonKho_ViewModel.Model.K_TonKho_ChiTiets)
            //        {
            //            bool IsHangHoaExist = false;
            //            for (int i = 0; i < countListHangTonVaNhap; i++)
            //            {
            //                if (listHangTonVaNhap[i][0].HangHoaId == item.HangHoaId)
            //                {
            //                    listHangTonVaNhap[i] = K_KhoInfo.NhapSauXuatTruocSort(listHangTonVaNhap[i]);
            //                    K_KhoInfo khoInfo = new K_KhoInfo(item.HangHoaId, item.SoLuong, item.DonGia);
            //                    listHangTonVaNhap[i].Add(khoInfo);
            //                    IsHangHoaExist = true;
            //                }
            //            }
            //            if (!IsHangHoaExist)
            //            {
            //                K_KhoInfo khoInfo = new K_KhoInfo(item.HangHoaId, item.SoLuong, item.DonGia);
            //                List<K_KhoInfo> listKhoInfo = new List<K_KhoInfo>();
            //                listKhoInfo.Add(khoInfo);
            //                listHangTonVaNhap.Add(listKhoInfo);
            //            }
            //        }
            //    }
            //    else
            //    {
            //        for (int i = 0; i < countListHangTonVaNhap; i++)
            //        {
            //            listHangTonVaNhap[i] = K_KhoInfo.NhapSauXuatTruocSort(listHangTonVaNhap[i]);
            //        }
            //    }
            //}

            //foreach (List<K_KhoInfo> ListXuatKhoItem in ListXuatKhoRutGon)
            //{
            //    int CountListHangTonVaNhap = listHangTonVaNhap.Count;
            //    for (int i = 0; i < CountListHangTonVaNhap; i++)
            //    {
            //        if (ListXuatKhoItem[0].HangHoaId == listHangTonVaNhap[i][0].HangHoaId)
            //        {
            //            foreach (K_KhoInfo XuatKhoItem in ListXuatKhoItem)
            //            {
            //                int CountHangTonVaNhap = listHangTonVaNhap[i].Count;
            //                double? RemainSoLuong = XuatKhoItem.SoLuong;
            //                for (int j = 0; j < CountHangTonVaNhap; j++)
            //                {
            //                    if (XuatKhoItem.Time.CompareTo(listHangTonVaNhap[i][j].Time) > 0 && listHangTonVaNhap[i][j].SoLuong != 0)
            //                    {
            //                        if (listHangTonVaNhap[i][j].SoLuong <= RemainSoLuong)
            //                        {
            //                            RemainSoLuong -= listHangTonVaNhap[i][j].SoLuong;
            //                            listHangTonVaNhap[i][j].SoLuong = 0;

            //                        }
            //                        else
            //                        {
            //                            listHangTonVaNhap[i][j].SoLuong -= RemainSoLuong;
            //                            break;
            //                        }
            //                    }
            //                }
            //            }
            //            break;  
            //        }
            //    }
            //}

            //foreach (List<K_KhoInfo> ListItem in listHangTonVaNhap)
            //{
            //    double TongSoLuongConlai = 0;
            //    double DonGiaChung = 0;
            //    foreach (K_KhoInfo Item in ListItem)
            //    {
            //        if (Item.SoLuong != 0 && Item.DonGia != 0 && Item.SoLuong != null && Item.DonGia != null)
            //        {
            //            TongSoLuongConlai += Item.SoLuong.Value;
            //            DonGiaChung += (Item.SoLuong.Value * Item.DonGia.Value);
            //        }
            //    }
            //    if (TongSoLuongConlai != 0)
            //    {
            //        DonGiaChung = DonGiaChung / TongSoLuongConlai;
            //        K_KhoInfo NewTonKhoInfo = new K_KhoInfo(ListItem[0].HangHoaId, TongSoLuongConlai, DonGiaChung);
            //        NewListTonKho.Add(NewTonKhoInfo);

            //    }
            //}

            //foreach (K_KhoInfo item in NewListTonKho)
            //{
            //    this.AddTonKhoDetailsItem();
            //}
        }

        public void OnCountChanged(object sender, EventArgs e)
        {
            IList<K_TonKho_ChiTietChild> listChiTiet = this.GetViewModel().Model.K_TonKho_ChiTiets;
            if (listChiTiet.Count == NewListTonKho.Count)
            {
                int index = 0;
                foreach (K_TonKho_ChiTietChild item in listChiTiet)
                {
                    item.HangHoaId = NewListTonKho[index].HangHoaId;
                    item.SoLuong = NewListTonKho[index].SoLuong;

                    System.Windows.Data.CollectionViewSource dm_HangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_HangHoa_ListViewModelSource"];
                    DM_HangHoa_ListViewModel dm_HangHoa_ListViewModel = ((List<object>)dm_HangHoa_CollectionViewSource.Source)[0] as DM_HangHoa_ListViewModel;
                    IList<DM_HangHoaInfo> listHangHoa = dm_HangHoa_ListViewModel.Model.getListOfInfo();

                    //foreach(DM_HangHoaInfo HangHoaItem in listHangHoa)
                    //{
                    //    if(HangHoaItem.Khoa == NewListTonKho[index].HangHoaId)
                    //    {
                    //        item.DienGiai = HangHoaItem.TenViet;
                    //        break;
                    //    }

                    //}
                    index++;
                }
            }
        }

        public void DoTinhTonKhoBinhQuanTucThi()
        {
            //    IList<K_ListNhapKhoInfo> listNhapKho = null;
            //    IList<K_ListXuatKhoInfo> listXuatKho = null;

            //    System.Windows.Data.CollectionViewSource k_ListNhapKho_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["K_ListNhapKho_ListViewModelSource"];
            //    K_ListNhapKho_ListViewModel k_ListNhapKho_ListViewModel = ((List<object>)k_ListNhapKho_CollectionViewSource.Source)[0] as K_ListNhapKho_ListViewModel;

            //    if (k_ListNhapKho_ListViewModel.Model != null)
            //    {
            //        listNhapKho = k_ListNhapKho_ListViewModel.Model.getListOfInfo();
            //    }

            //    System.Windows.Data.CollectionViewSource k_ListXuatKho_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["K_ListXuatKho_ListViewModelSource"];
            //    K_ListXuatKho_ListViewModel k_ListXuatKho_ListViewModel = ((List<object>)k_ListXuatKho_CollectionViewSource.Source)[0] as K_ListXuatKho_ListViewModel;

            //    if (k_ListXuatKho_ListViewModel.Model != null)
            //    {
            //        listXuatKho = k_ListXuatKho_ListViewModel.Model.getListOfInfo();
            //    }


            //    List<List<K_KhoInfo>> ListXuatKhoRutGon = new List<List<K_KhoInfo>>();

            //    if (listXuatKho != null)
            //    {
            //        foreach (K_ListXuatKhoInfo item in listXuatKho)
            //        {
            //            bool IsExist = false;
            //            foreach (List<K_KhoInfo> ListKhoInfo in ListXuatKhoRutGon)
            //            {
            //                if (ListKhoInfo[0].HangHoaId == item.HangHoaId)
            //                {
            //                    IsExist = true;
            //                    if (item.SoLuong != null)
            //                    {
            //                        K_KhoInfo khoInfo = new K_KhoInfo(item.HangHoaId, -1 * item.SoLuong, item.DonGia, item.NgayChungTu);
            //                        ListKhoInfo.Add(khoInfo);
            //                        break;

            //                    }

            //                }
            //            }
            //            if (!IsExist)
            //            {
            //                List<K_KhoInfo> NewListKhoInfo = new List<K_KhoInfo>();
            //                K_KhoInfo khoInfo = new K_KhoInfo(item.HangHoaId, -1 * item.SoLuong, item.DonGia, item.NgayChungTu);
            //                NewListKhoInfo.Add(khoInfo);
            //                ListXuatKhoRutGon.Add(NewListKhoInfo);
            //            }
            //        }
            //    }
            //    for (int i = 0; i < ListXuatKhoRutGon.Count; i++)
            //    {
            //        ListXuatKhoRutGon[i] = K_KhoInfo.NhapTruocXuatTruocSort(ListXuatKhoRutGon[i]);
            //    }

            //    List<List<K_KhoInfo>> listHangTonVaNhap = new List<List<K_KhoInfo>>();

            //    if (listNhapKho != null)
            //    {
            //        foreach (K_ListNhapKhoInfo item in listNhapKho)
            //        {
            //            bool IsExist = false;
            //            foreach (List<K_KhoInfo> ListKhoInfo in listHangTonVaNhap)
            //            {
            //                if (ListKhoInfo[0].HangHoaId == item.HangHoaId)
            //                {
            //                    IsExist = true;
            //                    if (item.SoLuong != null)
            //                    {
            //                        K_KhoInfo khoInfo = new K_KhoInfo(item.HangHoaId, item.SoLuong, item.DonGiaQĐ, item.NgayChungTu);
            //                        ListKhoInfo.Add(khoInfo);
            //                        break;

            //                    }

            //                }
            //            }
            //            if (!IsExist)
            //            {
            //                List<K_KhoInfo> NewListKhoInfo = new List<K_KhoInfo>();
            //                K_KhoInfo khoInfo = new K_KhoInfo(item.HangHoaId, item.SoLuong, item.DonGiaQĐ, item.NgayChungTu);
            //                NewListKhoInfo.Add(khoInfo);
            //                listHangTonVaNhap.Add(NewListKhoInfo);
            //            }
            //        }
            //    }


            //    System.Windows.Data.CollectionViewSource K_TonKho_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["K_TonKho_ThangTruoc_ViewModelSource"];
            //    K_TonKho_ViewModel K_TonKho_ViewModel = ((List<object>)K_TonKho_CollectionViewSource.Source)[0] as K_TonKho_ViewModel;


            //    int countListHangTonVaNhap = listHangTonVaNhap.Count;


            //    if (K_TonKho_ViewModel.Model.Khoa != 0)
            //    {
            //        foreach (K_TonKho_ChiTietChild item in K_TonKho_ViewModel.Model.K_TonKho_ChiTiets)
            //        {
            //            bool IsHangHoaExist = false;
            //            for (int i = 0; i < countListHangTonVaNhap; i++)
            //            {
            //                if (listHangTonVaNhap[i][0].HangHoaId == item.HangHoaId)
            //                {
            //                    listHangTonVaNhap[i] = K_KhoInfo.NhapTruocXuatTruocSort(listHangTonVaNhap[i]);

            //                    int numberOfDays = DateTime.DaysInMonth(this.GetViewModel().Model.Nam, this.GetViewModel().Model.Thang);
            //                    DateTime lastDay = new DateTime(this.GetViewModel().Model.Nam, this.GetViewModel().Model.Thang, numberOfDays);

            //                    K_KhoInfo khoInfo = new K_KhoInfo(item.HangHoaId, item.SoLuong, item.DonGia, lastDay.AddMonths(-1));
            //                    listHangTonVaNhap[i].Insert(0, khoInfo);
            //                    IsHangHoaExist = true;
            //                }
            //            }
            //            if (!IsHangHoaExist)
            //            {

            //                int numberOfDays = DateTime.DaysInMonth(this.GetViewModel().Model.Nam, this.GetViewModel().Model.Thang);
            //                DateTime lastDay = new DateTime(this.GetViewModel().Model.Nam, this.GetViewModel().Model.Thang, numberOfDays);

            //                K_KhoInfo khoInfo = new K_KhoInfo(item.HangHoaId, item.SoLuong, item.DonGia, lastDay.AddMonths(-1));
            //                List<K_KhoInfo> listKhoInfo = new List<K_KhoInfo>();
            //                listKhoInfo.Add(khoInfo);
            //                listHangTonVaNhap.Add(listKhoInfo);
            //            }
            //        }
            //    }
            //    else
            //    {
            //        for (int i = 0; i < countListHangTonVaNhap; i++)
            //        {
            //            foreach (List<K_KhoInfo> listItem in ListXuatKhoRutGon)
            //            {
            //                if (listItem[0].HangHoaId == listHangTonVaNhap[i][0].HangHoaId)
            //                {
            //                    listHangTonVaNhap[i].AddRange(listItem);
            //                    break;
            //                }
            //            }
            //            listHangTonVaNhap[i] = K_KhoInfo.NhapTruocXuatTruocSort(listHangTonVaNhap[i]);
            //        }
            //    }


            //    foreach (List<K_KhoInfo> listItem in listHangTonVaNhap)
            //    {
            //        double SoLuongTaiThoiDiemDangXet = 0;
            //        double DonGiaTaiThoiDiemDangXet = 0;
            //        foreach (K_KhoInfo item in listItem)
            //        {
            //            if (item.SoLuong < 0)
            //            {             
            //                SoLuongTaiThoiDiemDangXet -= (-1 * item.SoLuong.Value);
            //            }
            //            else
            //            {
            //                DonGiaTaiThoiDiemDangXet = (DonGiaTaiThoiDiemDangXet * SoLuongTaiThoiDiemDangXet + item.SoLuong.Value * item.DonGia.Value) / (item.SoLuong.Value + SoLuongTaiThoiDiemDangXet);
            //                SoLuongTaiThoiDiemDangXet += item.SoLuong.Value;
            //            }
            //        }
            //        if (SoLuongTaiThoiDiemDangXet != 0)
            //        {
            //            K_KhoInfo NewTonKhoInfo = new K_KhoInfo(listItem[0].HangHoaId, SoLuongTaiThoiDiemDangXet, DonGiaTaiThoiDiemDangXet);
            //            NewListTonKho.Add(NewTonKhoInfo);
            //        }
            //    }

            //    foreach (K_KhoInfo item in NewListTonKho)
            //    {
            //        this.AddTonKhoDetailsItem();
            //    }
            //}        
        }
    }
}
